package com.adjy.pathbuilders.objects;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.contacts.Contact;

import com.adjy.pathbuilders.physics.PhysicalObject;
import com.adjy.pathbuilders.rendering.AnimationPack;
import com.adjy.pathbuilders.sounds.SoundManager;
import com.adjy.pathbuilders.world.GameEntity;
import com.adjy.pathbuilders.world.GameWorld;
import com.adjy.pathbuilders.world.MainChar;

public class Spring extends GameEntity implements Thinkable {
	
	public static final float[] SIZE = {3, 3};
	
	private long animationStartTime;

	/**
	 * Creates a Spring
	 * 
	 * @param x
	 *            X Position
	 * @param y
	 *            Y Position
	 * @param frame
	 *            Frame to be used for rendering
	 */
	public Spring(float x, float y, AnimationPack ap) {
		super(GameEntityType.SPRING, x, y, SIZE[0] - 2, SIZE[1] - 2, ap, true, true, true);
		setAnimation("spring_idle");
		animationStartTime = 0;
		setRenderOffsets(-1f, 2f);
	}

	@Override
	public void hasStartedCollidingWith(PhysicalObject pObject, Contact contact) {
		
		if (pObject == null)
			return;

		if (((GameEntity) pObject).getType() == GameEntityType.MAINCHAR) { // Collided
																			// with
																			// Player
			SoundManager.getInstance().playSoundEffect("wall");
			
			MainChar mChar = (MainChar) pObject;
			mChar.getBody().setLinearVelocity(new Vec2(0,0));
			mChar.getBody().applyLinearImpulse(new Vec2(0, 80), mChar.getBody().getWorldCenter());
			animationStartTime = System.currentTimeMillis();
			setAnimation("spring_released");
			
		}
	}

	@Override
	public void think(GameWorld gameWorld, float timePassed) {
		if (animationStartTime > 0 && System.currentTimeMillis() - animationStartTime > 200) {
			animationStartTime = 0;
			setAnimation("spring_idle");
		}
		
	}
}
